﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using ExtAspNet;

namespace HY.WEB.SYS
{
    public partial class _area : PageBase
    {
        public string whe = "", stype = "";
        public string us ;
        public string tab = "pub_area";
        public string tabsub = "pub_area_sub";
       
        protected void Page_Load(object sender, EventArgs e)
        {  
            us=Session["ID"].ToString();
            if (!IsPostBack)
            {

              
              btnSearch.OnClientClick = Window1.GetShowReference("../function/search.aspx?tab="+tab+"", "高级搜索");

                fill();
                BindGrid();
                txtbh.Text = getmaxbh("");
            }
        }

        private void fill()
        {
            DataTable d = BLL.BLLfun.get_search(tab);
            ddlfiled.Items.Clear();
            for (int i = 0; i < d.Rows.Count; i++)
            {
                ddlfiled.Items.Add(new ExtAspNet.ListItem(d.Rows[i][1].ToString(), d.Rows[i][0].ToString()));
            }
            ddlfiled.DataBind();

            DataTable e = BLL.BLLfun.get_where();
            ddlwhere.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlwhere.Items.Add(new ExtAspNet.ListItem(e.Rows[i][0].ToString(), e.Rows[i][1].ToString()));
            }
            ddlwhere.DataBind();


;
       
            ddlarea.Items.Clear();
            DataTable du = BLL.Bllbase.get_table(tab);
            for (int i = 0; i < du.Rows.Count; i++)
            {
                ddlarea.Items.Add(new ExtAspNet.ListItem(du.Rows[i]["svalue"].ToString(), du.Rows[i]["scode"].ToString()));
            }
            ddlarea.DataBind();
            ddlarea.Items.Add(new ExtAspNet.ListItem("", ""));


            ddlarea.SelectedIndex = ddlarea.Items.IndexOf(ddlarea.Items.FindByValue(""));

            ////填充角色
            //BLL.HR.hr_ot s = new BLL.HR.hr_ot();
            //e = s.getlist_role(whe);
            //ddlrole.Items.Clear();
            //for (int i = 0; i < e.Rows.Count; i++)
            //{
            //    ddlrole.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            //}
            //ddlrole.DataBind();
            //ddlrole.Items.Add(new ExtAspNet.ListItem("",""));
           
        }

        public  string getnm(string nmid)
        {
            BLL.sys.sys_user u = new BLL.sys.sys_user();
            return u.getname(nmid);
         }

        private void BindGrid()
        {
            DataTable  table2;

            table2 = BLL.Bllbase.get_table(tabsub);
            Grid2.DataSource = table2;
            Grid2.DataBind();

            DataTable table3 = BLL.Bllbase.get_table(tab);
            Grid3.DataSource = table3;
            Grid3.DataBind();
        }

      

        protected void Grid2_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid2.PageIndex = e.NewPageIndex;
        }

        protected void Grid3_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid3.PageIndex = e.NewPageIndex;
        }


        protected void Grid2_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {

            DataTable table = BLL.Bllbase.get_table(tabsub);
            //DataTable table = BLL.oa.ad.get_list(stype);

            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid2.DataSource = view1;
            Grid2.DataBind();
        }

        protected void Grid3_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {

            DataTable table = BLL.Bllbase.get_table(tab);

            //DataTable table = BLL.oa.ad.get_list(stype);

            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid3.DataSource = view1;
            Grid3.DataBind();
        }


        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger2Click(object sender, EventArgs e)
        {
            txt.ShowTrigger1 = true;
            string ddlf = ddlfiled.SelectedValue;
            string ddlw = ddlwhere.SelectedValue;
            string key = txt.Text;
            if (ddlw.Equals("="))
                whe = "" + ddlf + " like  '%" + key + "%'";
            else
            {
                if (ddlf == "sid" || ddlf == "编号")
                    whe = "" + ddlf + " " + ddlw + " " + key + "";
                else
                    whe = "" + ddlf + " " + ddlw + " '" + key + "'";

            }
            if (key == "") whe = "";

            BindGrid();
        }

        /// <summary>
        /// 清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger1Click(object sender, EventArgs e)
        {
            txt.Text = String.Empty;
            txt.ShowTrigger1 = false;
            BindGrid();
        }

        //删除从表
        protected void btnDelete2_Click(object sender, EventArgs e)
        {
            int val = Grid2.PageIndex * Grid2.PageSize;
            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid2.SelectedRowIndexArray)
            {
               string DD = Grid2.DataKeys[row+val][0].ToString();
                sb.Append(Grid2.DataKeys[row][0].ToString());
                sb.Append(",");
            }

            Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));
            HY.BLL.Bllbase.DeleteList(tabsub, sb.ToString().TrimEnd(','));
            BindGrid();
        }

        //删除主表
        protected void btnDelete3_Click(object sender, EventArgs e)
        {
            int val = Grid3.PageIndex * Grid3.PageSize;

            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid3.SelectedRowIndexArray)
            {
                sb.Append(Grid3.DataKeys[row+val][1].ToString());
                sb.Append(",");
            }

            Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));
            HY.BLL.Bllbase.DeleteList(tab, sb.ToString().TrimEnd(','));
            BindGrid();

        }

        protected void Grid2_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName == "edit") //修改 
            { 
             
                string DD = Grid2.DataKeys[e.RowIndex][0].ToString();
              
                DataTable dt1 = BLL.Bllbase.get_table(tabsub,"sid=" + DD + "");
           
                hidsid.Text = dt1.Rows[0]["sid"].ToString().Trim();

                string t = dt1.Rows[0]["scode"].ToString().Trim();

                t = t.Substring(0, 4);
                ddlarea.SelectedIndex = ddlarea.Items.IndexOf(ddlarea.Items.FindByValue(t));
                txtbh.Text = dt1.Rows[0]["scode"].ToString().Trim();
                txtnm.Text = dt1.Rows[0]["svalue"].ToString().Trim();
                                

                // ddluser.SelectedIndex = 
                //     ddluser.Items.IndexOf(ddluser.Items.FindByValue(getnm( dt1.Rows[0]["renm"].ToString().Trim())));
              
                //ddlrole.SelectedIndex =
                //         ddlrole.Items.IndexOf(ddlrole.Items.FindByValue(dt1.Rows[0]["role"].ToString().Trim()));
           
            
            }
            

        }

        protected void Grid3_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            string DD = Grid3.DataKeys[e.RowIndex][0].ToString();
            if (e.CommandName == "sub") //修改 
            {            
              
                DataTable dt1 = BLL.Bllbase.get_table(tabsub,"left(scode,4)='" + DD + "'");
                Grid2.DataSource = dt1;
                Grid2.DataBind();
            }
            if (e.CommandName == "edit") //修改 
            {
              
                DataTable dt1 = BLL.Bllbase.get_table(tab, "scode=" + DD + "");

                hidsid.Text = dt1.Rows[0]["sid"].ToString().Trim();

                ddlarea.SelectedIndex = ddlarea.Items.IndexOf(ddlarea.Items.FindByValue(""));
                txtbh.Text = dt1.Rows[0]["scode"].ToString().Trim();
                txtnm.Text = dt1.Rows[0]["svalue"].ToString().Trim();

            }
        }

        protected void Window1_Close(object sender, EventArgs e)
        {
            // Alert.ShowInTop("弹出窗口关闭了！");
            string key = txt.Text;
          //  stype = hidstype.Text;
            try
            {
                if (key == "") whe = BLL.pub.pubwhere.get_where_sql(Session["ID"].ToString(), tab);
                BindGrid();

            }
            catch (Exception ex)
            {
                Alert.Show("查询条件错误");
            }
            finally
            {
                whe = "";
            }
        }

        protected void btsave_Click(object sender, EventArgs e)
        {
            string user=us;
            string sqltxt = "";
            string sheng =ddlarea.SelectedValue;
            string bh = txtbh.Text;
            string nm =txtnm.Text;
          
            if (bh==""|nm=="")
            {
                return;
            }


            if (hidsid.Text== "")
            {
                if (sheng == "")
                    sqltxt = "insert into " + tab + "  values('" + bh + "','" + nm + "')";
                else
                    sqltxt = "insert into " + tabsub + "  values('" + bh + "','" + nm + "')";

            }
            else
            {
                if (sheng == "")
                    sqltxt = "update  " + tab + "  set scode='" + bh + "',svalue='" + nm +
                        "' where sid="+hidsid.Text+"";
                else
                    sqltxt = "update  " + tabsub + "  set scode='" + bh + "',svalue='" + nm + 
                        "' where sid=" + hidsid.Text + "";
            }

            DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);

            hidsid.Text = "";

            ddlarea.SelectedIndex = ddlarea.Items.IndexOf(ddlarea.Items.FindByValue(""));
            txtbh.Text = getmaxbh("");
            txtnm.Text="";
          
            BindGrid();
        }

        string getmaxbh(string sh)
        {
            string sqltxt = "";
            DataTable dt;
            string val = "";

            if (sh == "")
                sqltxt = "select max(scode) from  " + tab + "  ";
            else
                sqltxt = "select max(scode) from  " + tabsub + "  where left(scode,4)='" + sh + "' ";
           
             dt=DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
            if (dt.Rows.Count>0)
                val=(int.Parse(dt.Rows[0][0].ToString())+1).ToString();

            return val;
        }

        protected void ddlarea_SelectedIndexChanged(object sender, EventArgs e)
        {
          if (ddlarea.SelectedValue!="")  txtbh.Text = getmaxbh(ddlarea.SelectedValue);
        }
    
    }
}
